package com.woniumall.web;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.woniumall.pojo.*;
import com.woniumall.service.AddressService;
import com.woniumall.service.OrderService;
import com.woniumall.service.impl.AddressServiceImpl;
import com.woniumall.service.impl.OrderServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

/**
 * @auther linke
 * @create 2021-11-02 9:51
 */
@WebServlet("/personal/*")
public class PersonalServlet extends BaseServlet{
    private OrderService orderService = new OrderServiceImpl();
    private AddressService addressService = new AddressServiceImpl();
    //用户登录时，跳转至个人中心主页
    public void index(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String index = "index";
        request.setAttribute("index",index);
        //获取域中的用户名
        User user = (User) request.getSession().getAttribute("user");
        int uid = user.getId();//得到用户ID值
        PageInfo<Order> plist =   orderService.findAllOrder(1,uid);//查询第一页订单，显示条数4条
        request.setAttribute("orderlist",plist);
        request.getRequestDispatcher("/jspPage/personal1.jsp").forward(request,response);
    }
    //删除订单，回滚库存
    public void delOrder(HttpServletRequest request, HttpServletResponse response) throws Exception {
//        String goodsid = request.getParameter("goodsid");//图书id
        String orderno = request.getParameter("orderno");//订单id

    /*    System.out.println(Json);
        //增加订单数据校验
        if(Json == null ){return; }
        //System.out.println(Json);*/
/*        row = 0;
        //将json数据转换成int数组
        int[] ids = new ObjectMapper().readValue(Json.getBytes(), int[].class);*/

        int   row = orderService.delOrderAndUodateGoodsNum(orderno);


        Massage msg = new Massage();
        if (row < 1){
            msg.setFlag(false);
            msg.setMassage("订单取消失败，请联系管理员！");
            responseUtil(response,msg);
        }else {//删除成功
            msg.setFlag(true);
            responseUtil(response,msg);
        }
    }
    //添加地址
    public void add(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取域中的用户名
        User user = (User) request.getSession().getAttribute("user");
        int uid = user.getId();//得到用户ID值
        //获取输入框数据
        String accept = request.getParameter("accept");//获取手机号
        String phone = request.getParameter("phone");//获取手机号
        String cmbProvince = request.getParameter("cmbProvince");//获取省份
        String cmbCity = request.getParameter("cmbCity");//获取城市
        String cmbArea = request.getParameter("cmbArea");//获取区域
        String addres = request.getParameter("addres");//获取详细地址
        String defaultBox = request.getParameter("rideo");//获取默认状态的设置
        if(defaultBox == ""){
            defaultBox = "0";
        }
        //封装
        Address add = new Address();
        add.setUserid(uid);
        add.setAccept(accept);
        add.setTelphone(phone);
        add.setProvince(cmbProvince);
        add.setCity(cmbCity);
        add.setArea(cmbArea);
        add.setAddress(addres);
        add.setType(defaultBox);
        System.out.println(add);

        //传递下一层
        int addressid = addressService.addressUp(add);

        if(defaultBox == "1" ){//创建地址时勾选默认，调用更新状态方法
            addressService.updateType(addressid,user.getId());//更新地址状态
        }
        //重新加载当前页面
        address(request,response);
      /*  response.sendRedirect("/woniusales/page/vipPage.jsp");*/
    }
    //修改默认地址状态
    public void addressUpdate(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String addressid = request.getParameter("addressid");//获取页面设置默认的地址id
        User user =(User) request.getSession().getAttribute("user");
        addressService.updateType(Integer.parseInt(addressid),user.getId());//更新地址状态
        //重新加载当前页面
        address(request,response);
    }
    //地址管理
    public void address(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String address = "address";
        request.setAttribute("address",address);
        User user = (User)request.getSession().getAttribute("user");
        AddressService addressService = new AddressServiceImpl();
        List<Address> addlist = addressService.findAllByUserId(user.getId());
        request.setAttribute("addlist",addlist);
        request.getRequestDispatcher("/jspPage/personal1.jsp").forward(request,response);
    }
    //订单首页管理
    public void orderShowPage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String order = "order";
        request.setAttribute("order",order);
        //获取域中的用户名
        User user = (User) request.getSession().getAttribute("user");
        int uid = user.getId();//得到用户ID值
        String num = request.getParameter("num");
        PageInfo<Order> plist =   orderService.findAllOrder(Integer.parseInt(num),uid);
        request.setAttribute("orderList",plist);
        request.getRequestDispatcher("/jspPage/personal1.jsp").forward(request,response);
    }

/*    public void orderShow(HttpServletRequest request, HttpServletResponse response) throws Exception {
        PageInfo<Order> plist =   orderService.findAllOrder(1);
        request.setAttribute("orderList",plist);
        String order = "order";
        request.setAttribute("order",order);
        request.getRequestDispatcher("/jspPage/personal1.jsp").forward(request,response);
    }*/


    //密码修改
    public void password(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String password = "password";
        request.setAttribute("password",password);
        request.getRequestDispatcher("/jspPage/personal1.jsp").forward(request,response);
    }
    //头像修改
    public void headimage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String headimage = "headimage";
        request.setAttribute("headimage",headimage);
        request.getRequestDispatcher("/jspPage/personal1.jsp").forward(request,response);
    }

    //订单详情
    public void orderDetail(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String orderDetail = "orderDetail";
        request.setAttribute("orderDetail",orderDetail);//设置详情页面展示

        String orderid = request.getParameter("orderid");
        Order order = orderService.findOrderById(Integer.parseInt(orderid));//查询订单基本信息
        List<OrderItem> orderItemList = orderService.findOrderItemById(Integer.parseInt(orderid));//查询订单相关书籍详情信息

        request.setAttribute("ordermsg",order);//存储在request域中

        request.setAttribute("orderItemList",orderItemList);//存储在request域中

        request.getRequestDispatcher("/jspPage/personal1.jsp").forward(request,response);
    }



}
